package fr.kotton.myorganizer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import fr.kotton.myorganizer.debug.ViewServer;
import fr.kotton.myorganizer.fragment.ContactsFragment;
import fr.kotton.myorganizer.fragment.DiaryMonthlyFragment;
import fr.kotton.myorganizer.fragment.NavigationDrawerFragment;
import fr.kotton.myorganizer.task.fragment.ListTasksFragment;
import fr.kotton.myorganizer.utils.Consts;


public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private DiaryMonthlyFragment mDiaryMonthlyFragment;
    private ListTasksFragment mListTasksFragment;
    private ContactsFragment mContactsFragment;

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /********** Debug **********/
        ViewServer.get(this).addWindow(this);
        /***************************/

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int pPosition) {

        FragmentManager fragmentManager = getSupportFragmentManager();

        fragmentManager.beginTransaction()
                .replace(R.id.container, loadInstance(pPosition + 1))
                .commit();

    }

    public void onSectionAttached(int pNumber) {
        switch (pNumber) {
            case Consts.FRAGMENT_DIARY_DAILY_REF_INT:
                mTitle = getString(R.string.section_title_diary) + " - " + getString(R.string.section_title_diary_daily);
                break;
            case Consts.FRAGMENT_DIARY_WEEKLY_REF_INT:
                mTitle = getString(R.string.section_title_diary) + " - " + getString(R.string.section_title_diary_weekly);
                break;
            case Consts.FRAGMENT_DIARY_MONTHLY_REF_INT:
                mTitle = getString(R.string.section_title_diary) + " - " + getString(R.string.section_title_diary_monthly);
                break;
            case Consts.FRAGMENT_TASKS_REF_INT:
                mTitle = getString(R.string.section_title_tasks);
                break;
            case Consts.FRAGMENT_NOTES_REF_INT:
                mTitle = getString(R.string.section_title_notes);
                break;
            case Consts.FRAGMENT_CONTACTS_REF_INT:
                mTitle = getString(R.string.section_title_contacts);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.mainactivity_action_bar, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public Fragment newInstance(int pSectionNumber) {

        Fragment lFragment = null;
        Bundle lArgs = new Bundle();
        lArgs.putInt(Consts.FRAGMENT_REF_STRING, pSectionNumber);

        if (pSectionNumber == Consts.FRAGMENT_DIARY_MONTHLY_REF_INT) {

            // If necessary add specific elements to the bundle here
            lFragment = new DiaryMonthlyFragment();

        } else if (pSectionNumber == Consts.FRAGMENT_TASKS_REF_INT) {

            // If necessary add specific elements to the bundle here
            lFragment = new ListTasksFragment();

        } else if (pSectionNumber == Consts.FRAGMENT_CONTACTS_REF_INT) {

            // If necessary add specific elements to the bundle here
            lFragment = new ContactsFragment();

        } else {
            // If necessary add specific elements to the bundle here
            lFragment = new DiaryMonthlyFragment();
        }

        lFragment.setArguments(lArgs);

        return lFragment;
    }

    /**
     * Load the instance of the fragment for the given section
     * number. Instanciate a new one if it does not exist.
     */
    public Fragment loadInstance(int pSectionNumber) {

        if (pSectionNumber == Consts.FRAGMENT_DIARY_MONTHLY_REF_INT) {
            if (mDiaryMonthlyFragment != null) {
                return mDiaryMonthlyFragment;
            } else {
                mDiaryMonthlyFragment = (DiaryMonthlyFragment) newInstance(pSectionNumber);
                return mDiaryMonthlyFragment;
            }
        } else if (pSectionNumber == Consts.FRAGMENT_TASKS_REF_INT) {
            if (mListTasksFragment != null) {
                return mListTasksFragment;
            } else {
                mListTasksFragment = (ListTasksFragment) newInstance(pSectionNumber);
                return mListTasksFragment;
            }
        } else if (pSectionNumber == Consts.FRAGMENT_CONTACTS_REF_INT) {
            if (mContactsFragment != null) {
                return mContactsFragment;
            } else {
                mContactsFragment = (ContactsFragment) newInstance(pSectionNumber);
                return mContactsFragment;
            }
        } else {
            mDiaryMonthlyFragment = (DiaryMonthlyFragment) newInstance(pSectionNumber);
            return mDiaryMonthlyFragment;
        }
    }

    /**
     * ******* Debug *********
     */
    public void onDestroy() {
        super.onDestroy();
        ViewServer.get(this).removeWindow(this);
    }

    public void onResume() {
        super.onResume();
        ViewServer.get(this).setFocusedWindow(this);
    }
    /***************************/
}
